package com.hghivln.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hghivln.blog.entity.Blog;
import com.hghivln.blog.entity.BlogGoods;
import com.hghivln.blog.mapper.BlogGoodsMapper;
import com.hghivln.blog.mapper.BlogMapper;
import com.hghivln.blog.service.BlogGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.blog.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户_博客点赞表 服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-09-25
 */
@Service
public class BlogGoodsServiceImpl extends ServiceImpl<BlogGoodsMapper, BlogGoods> implements BlogGoodsService {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public void goods(Long blogId) {
        Long id = ShiroUtils.getLoginUser().getId();
        BlogGoods blogGoods = new BlogGoods();
        blogGoods.setBlogId(blogId);
        blogGoods.setUserId(id);
        getBaseMapper().insert(blogGoods);
        Blog blog = blogMapper.selectById(blogId);
        blog.setBlogGoods(blog.getBlogGoods()+1);
        blogMapper.updateById(blog);
    }

    @Override
    public Integer getGoods(Long blogId) {
        Long id = ShiroUtils.getLoginUser().getId();
        return getBaseMapper().getCountByBlogIdAndUserId(blogId,id);
    }

    @Override
    public void removeByBlogId(Long blogId) {
        QueryWrapper<BlogGoods> qw = new QueryWrapper<>();
        Long id = ShiroUtils.getLoginUser().getId();
        qw.eq("blog_id",blogId);
        qw.eq("user_id",id);
        getBaseMapper().delete(qw);
        Blog blog = blogMapper.selectById(blogId);
        blog.setBlogGoods(blog.getBlogGoods()-1);
        blogMapper.updateById(blog);
    }
}
